package kmp;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * 给定一个只包括 '('，')'，'{'，'}'，'['，']' 的字符串 s ，判断字符串是否有效。
 */
public class leetCode20_isValid {
    public static void main(String[] args) {
        leetCode20_isValid a= new leetCode20_isValid();
        boolean valid = a.isValid("()");
        System.out.println(valid);
    }
    public boolean isValid(String s) {
        if(s == null) return false;

        Stack<Character> stack = new Stack<>();
        char[] chars = s.toCharArray();
        for (char aChar : chars) {
            if(stack.isEmpty()){
                stack.add(aChar);
                continue;
            }
            Character peek = stack.peek();
            if(!delete(peek,aChar)){
                stack.add(aChar);
            } else{
                stack.pop();
            }
        }
        return stack.isEmpty();


    }

    public boolean delete(Character a, Character b){
        if(a == '(' && b == ')'){
            return true;
        }

        if(a == '[' && b == ']'){
            return true;
        }

        if(a == '{' && b == '}'){
            return true;
        }
        return false;
    }



}
